/***************************************************************************
 *   oboe.controllers.soundController.ManagedChannel                       *
 *   Copyright (C) 2011 by Felipe Manga                                    *
 *   lufeboma@gmail.com                                                    *
 *                                                                         *
 *   Permission is hereby granted, free of charge, to any person obtaining *
 *   a copy of this software and associated documentation files (the       *
 *   "Software"), to deal in the Software without restriction, including   *
 *   without limitation the rights to use, copy, modify, merge, publish,   *
 *   distribute, sublicense, and/or sell copies of the Software, and to    *
 *   permit persons to whom the Software is furnished to do so, subject to *
 *   the following conditions:                                             *
 *                                                                         *
 *   The above copyright notice and this permission notice shall be        *
 *   included in all copies or substantial portions of the Software.       *
 *                                                                         *
 *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,       *
 *   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF    *
 *   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
 *   IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR     *
 *   OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
 *   ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR *
 *   OTHER DEALINGS IN THE SOFTWARE.                                       *
 ***************************************************************************/

package oboe.controllers.soundController
{
	import flash.events.Event;
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.media.SoundTransform;
	
	import oboe.core.Oboe;

	public class ManagedChannel
	{
		private var channel:SoundChannel;
		private var loops:int;
		private var transform:SoundTransform;
		private var position:Number;
		private var onEnd:Function;
		private var sound:ManagedSound;

		private var isMuted:Boolean = false;
		private var volume:Number = 1;
		
		public function ManagedChannel( sound:ManagedSound, pos:Number, loops:int, transform:SoundTransform, onEnd:Function )
		{
			this.sound = sound;
			this.loops = loops;
			if( loops == -1 ) loops = 0x7FFFFFFF;
			if( !transform )
			{
				if( Oboe.flags["SOUNDMUTED"] )
				{
					transform = new SoundTransform(0);
				}
				else
				{
					transform = new SoundTransform(1);
				}
				this.volume = 1;
			}
			else
			{
				this.volume = transform.volume;
				if( Oboe.flags["SOUNDMUTED"] )
				{
					transform = new SoundTransform(0);
				}
			}
			this.transform = transform;
			this.channel = sound.getSound().play( pos, loops, transform );
			this.position = pos;
			this.onEnd = onEnd;
			if(this.channel)
				this.addEventListeners( );
			else
				this.onEnd(this.sound, this);
		}
		
		private function addEventListeners():void
		{
			if( this.loops == -1 )
			{
				channel.addEventListener(Event.SOUND_COMPLETE, function(e:Event):void{
					stop( false );
					position = 0;
					resumeAfter(0);
				} );
			}
		}
		
		public function stop( callOnEnd:Boolean ):void
		{
			if( !this.channel ) return;
			if( callOnEnd && onEnd != null ) onEnd( sound, this );
			this.position = channel.position;
			this.channel.stop();
			this.channel = null;
		}
		
		public function resumeAfter( skip:Number ):void
		{
			this.stop( false );
			var loops:int = this.loops;
			var s:Sound = sound.getSound();
			var over:Number = (this.position+skip) % s.length;
			if( this.position+skip )
			{
				loops = 1;
			}
			else
			{
				if( this.loops == -1 )
				{
					loops = 0x7FFFFFFF;
				}
				else
				{
					this.loops -= this.position - s.length;
				}
			}
			this.position = over;
			this.channel = s.play( this.position, loops, this.transform );
			this.addEventListeners( );
		}
		
		public function setMute( muted:Boolean ):void
		{
			if( !this.channel ) return;
			
			if( !this.transform )
			{
				this.transform = new SoundTransform();
			}
			
			if( muted )
			{
				if( this.isMuted ) return;
				volume = this.transform.volume;
				this.transform.volume = 0;
			}
			else
			{
				if( !this.isMuted ) return;
				this.transform = new SoundTransform( this.volume );
			}
			this.isMuted = muted;
			this.channel.soundTransform = this.transform;
		}
	}
}